package top.scauwlt;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.scauwlt.config.RpcConfig;
import top.scauwlt.factory.SingletonFactory;
import top.scauwlt.provider.ServiceProvider;
import top.scauwlt.provider.impl.ServiceProviderImpl;
import top.scauwlt.spring.SpringBeanPostProcessor;

/**
 * @Author wmm
 * @Date 2022/7/28 21:57
 */
@Configuration
@EnableConfigurationProperties(RpcProperties.class)
public class RpcSeverAutoConfiguration {
    @Autowired
    private RpcProperties rpcProperties;

    @Bean
    public ServiceProvider serviceProvider() {
        if (rpcProperties != null) {
            RpcProperties.RegistryProperties registry = rpcProperties.getRegistry();
            if (registry != null) {
                if (registry.getName() != null) {
                    RpcConfig.getInstance().setRegistryType(registry.getName());
                    if (registry.getName().equals("nacos")) {
                        RpcConfig.getInstance().setRegistryAddress("127.0.0.1:8848");
                    }
                }
                if (registry.getAddress() != null) {
                    RpcConfig.getInstance().setRegistryAddress(registry.getAddress());
                }
            }
        }
        return SingletonFactory.getInstance(ServiceProviderImpl.class);
    }

    @Bean
    public RpcServerProvider rpcServerProvider() {
        return new RpcServerProvider(rpcProperties);
    }
}
